﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace My3DGame
{
    public class clsMenu
    {
        #region Thuộc tính

        string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        clsMenu parentItems;
        public clsMenu ParentItems
        {
            get { return parentItems; }
            set { parentItems = value; }
        }

        List<clsMenu> subItems;
        public List<clsMenu> SubItems
        {
            get { return subItems; }
            set { subItems = value; }
        }

        int choiceIndex;
        public int ChoiceIndex
        {
            get { return choiceIndex; }
            set { choiceIndex = value; }
        }

        #endregion

        public clsMenu()
        {
            name = null;
            parentItems = null;
            subItems = new List<clsMenu>();
            choiceIndex = 0;
        }

        static public void ShowMenu(clsMenu menu)
        {
            Console.WriteLine(menu.Name);
            for (int i = 0; i < menu.SubItems.Count; i++)
            {
                ShowMenu(menu.SubItems[i]);
            }
        }

        static public clsMenu LoadFromXML(string menuFile)
        {
            clsMenu menu = new clsMenu();
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(menuFile);

                menu.Name = xmlDoc.DocumentElement.GetAttribute("name");
                XmlNodeList menuNodeList = xmlDoc.DocumentElement.ChildNodes;
                if (xmlDoc.DocumentElement.ChildNodes.Count > 0)
                {
                    menu.SubItems = LoadAllMenuItems(xmlDoc.DocumentElement);
                }
            }
            catch (Exception e)
            {
                clsErrorMsgHelper.WriteError("Can't load\nDetail:\n" + e.Message);
            }

            return menu;
        }

        static public List<clsMenu> LoadAllMenuItems(XmlElement menuRoot)
        {
            List<clsMenu> menus = new List<clsMenu>();
            XmlNodeList menuNodeList = menuRoot.ChildNodes;
            foreach (XmlElement menuElement in menuNodeList)
            {
                clsMenu temp = new clsMenu();
                temp.Name = menuElement.GetAttribute("name");
                if (menuElement.ChildNodes.Count > 0)
                {
                    temp.SubItems = LoadAllMenuItems(menuElement);
                }
                menus.Add(temp);
            }
            return menus;
        }
    }
}
